home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / gfx / show / gs_src_amiga.lha / gdevpng.c < prev    next >
C/C++ Source or Header  |  1996-01-19  |  8KB  |  262 lines

  1. /* Copyright (C) 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpng.c */
  20. /* PNG (Portable Network Graphics) Format.  Pronounced "ping". */
  21. /* Russell Lang 1995-07-04 */
  22.  
  23. #include "gdevprn.h"
  24. #include "gdevpccm.h"
  25. #include "gscdefs.h"
  26.  
  27. #define PNG_INTERNAL
  28. #include "png.h"
  29.  
  30. /* ------ The device descriptors ------ */
  31.  
  32. /*
  33.  * Default X and Y resolution.
  34.  */
  35. #define X_DPI 72
  36. #define Y_DPI 72
  37.  
  38. private dev_proc_print_page(png_print_page);
  39.  
  40. /* Monochrome. */
  41.  
  42. gx_device_printer far_data gs_pngmono_device =
  43.   prn_device(prn_std_procs, "pngmono",
  44.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  45.     X_DPI, Y_DPI,
  46.     0,0,0,0,            /* margins */
  47.     1, png_print_page);
  48.  
  49. /* 4-bit planar (EGA/VGA-style) color. */
  50.  
  51. private gx_device_procs png16_procs =
  52.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  53.     pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  54. gx_device_printer far_data gs_png16_device =
  55.   prn_device(png16_procs, "png16",
  56.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  57.     X_DPI, Y_DPI,
  58.     0,0,0,0,            /* margins */
  59.     4, png_print_page);
  60.  
  61. /* 8-bit (SuperVGA-style) color. */
  62. /* (Uses a fixed palette of 3,3,2 bits.) */
  63.  
  64. private gx_device_procs png256_procs =
  65.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  66.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  67. gx_device_printer far_data gs_png256_device =
  68.   prn_device(png256_procs, "png256",
  69.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  70.     X_DPI, Y_DPI,
  71.     0,0,0,0,            /* margins */
  72.     8, png_print_page);
  73.  
  74. /* 8-bit gray */
  75.  
  76. private gx_device_procs pnggray_procs =
  77.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  78.     gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
  79. gx_device_printer far_data gs_pnggray_device =
  80. { prn_device_body(gx_device_printer, pnggray_procs, "pnggray",
  81.            DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  82.            X_DPI, Y_DPI,
  83.            0,0,0,0,            /* margins */
  84.            1,8,255,0,256,0, png_print_page)
  85. };
  86.  
  87. /* 24-bit color. */
  88.  
  89. private dev_proc_map_rgb_color(map_16m_rgb_color);
  90. private dev_proc_map_color_rgb(map_16m_color_rgb);
  91. private gx_device_procs png16m_procs =
  92.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  93.     map_16m_rgb_color, map_16m_color_rgb);
  94. gx_device_printer far_data gs_png16m_device =
  95.   prn_device(png16m_procs, "png16m",
  96.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  97.     X_DPI, Y_DPI,
  98.     0,0,0,0,            /* margins */
  99.     24, png_print_page);
  100.  
  101.  
  102. /* ------ Private definitions ------ */
  103.  
  104. /* Write out a page in PNG format. */
  105. /* This routine is used for all formats. */
  106. private int
  107. png_print_page(gx_device_printer *pdev, FILE *file)
  108. {    int raster = gdev_prn_raster(pdev);
  109.     /* PNG structures */
  110.     png_struct *png_ptr;
  111.     png_info *info_ptr;
  112.     byte *row = (byte *)gs_malloc(raster, 1, "png raster buffer");
  113.     int height = pdev->height;
  114.     int depth = pdev->color_info.depth;
  115.     int y;
  116.     int code = 0;            /* return code */
  117.     char *software_key="Software";
  118.     char software_text[256];
  119.     png_text text_png;
  120.  
  121.     if ( row == 0 )            /* can't allocate row buffer */
  122.       return_error(gs_error_VMerror);
  123.  
  124.     /* allocate the necessary structures */
  125.     png_ptr = gs_malloc(sizeof (png_struct), 1, "png structure");
  126.     if (!png_ptr)
  127.       return_error(gs_error_VMerror);
  128.  
  129.     info_ptr = gs_malloc(sizeof (png_info), 1, "png info_ptr");
  130.     if (!info_ptr)
  131.       {
  132.         gs_free((char *)row, raster, 1, "png raster buffer");
  133.         gs_free((char *)png_ptr, sizeof (png_struct), 1, "png structure");
  134.         return_error(gs_error_VMerror);
  135.       }
  136.  
  137.     /* set error handling */
  138.     if (setjmp(png_ptr->jmpbuf))
  139.     {
  140.         png_write_destroy(png_ptr);
  141.         gs_free((char *)row, raster, 1, "png raster buffer");
  142.         gs_free((char *)png_ptr, sizeof (png_struct), 1, "png structure");
  143.         gs_free((char *)info_ptr, sizeof (png_info), 1, "png info_ptr");
  144.         /* If we get here, we had a problem reading the file */
  145.         return_error(gs_error_VMerror);
  146.     }
  147.  
  148.     /* initialize the structures */
  149.     png_info_init(info_ptr);
  150.     png_write_init(png_ptr);
  151.  
  152.     /* set up the output control */
  153.     png_init_io(png_ptr, file);
  154.  
  155.     /* set the file information here */
  156.     info_ptr->width = pdev->width;
  157.     info_ptr->height = pdev->height;
  158.     switch(depth) {
  159.         case 24:
  160.             info_ptr->bit_depth = 8;
  161.         info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  162.         break;
  163.         case 8:
  164.         info_ptr->bit_depth = 8;
  165.         if (gx_device_has_color(pdev))
  166.             info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  167.         else
  168.             info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  169.         break;
  170.         case 4:
  171.         info_ptr->bit_depth = 4;
  172.         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  173.         break;
  174.         case 1:
  175.         info_ptr->bit_depth = 1;
  176.         info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  177.         /* invert monocrome pixels */
  178.            png_set_invert_mono(png_ptr);
  179.         break;
  180.     }
  181.  
  182.     /* set the palette if there is one */
  183.     if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
  184.         int i;
  185.         int num_colors = 1<<depth;
  186.         gx_color_value rgb[3];
  187.         info_ptr->valid |= PNG_INFO_PLTE;
  188.         info_ptr->palette = gs_malloc(256 * sizeof (png_color), 1, "png palette");
  189.         info_ptr->num_palette = num_colors;
  190.         for (i=0; i<num_colors; i++) {
  191.         (*dev_proc(pdev, map_color_rgb))((gx_device *)pdev,
  192.                 (gx_color_index)i, rgb);
  193.         info_ptr->palette[i].red = gx_color_value_to_byte(rgb[0]);
  194.         info_ptr->palette[i].green = gx_color_value_to_byte(rgb[1]);
  195.         info_ptr->palette[i].blue = gx_color_value_to_byte(rgb[2]);
  196.         }
  197.     }
  198.  
  199.     /* add comment */
  200.     sprintf(software_text, "%s %d.%02d", gs_product, 
  201.         (int)(gs_revision / 100), (int)(gs_revision % 100));
  202.     text_png.compression = -1;    /* uncompressed */
  203.     text_png.key = software_key;
  204.     text_png.text = software_text;
  205.     text_png.text_length = strlen(software_text);
  206.     info_ptr->text = &text_png;
  207.     info_ptr->num_text = 1;
  208.  
  209.     /* write the file information */
  210.     png_write_info(png_ptr, info_ptr);
  211.  
  212.     /* don't write the comments twice */
  213.     info_ptr->num_text = 0;
  214.     info_ptr->text = NULL;
  215.  
  216.     /* Write the contents of the image. */
  217.     for ( y = 0; y < height; y++ )
  218.     {    gdev_prn_copy_scan_lines(pdev, y, row, raster);
  219.              png_write_rows(png_ptr, &row, 1);
  220.     }
  221.  
  222.     /* write the rest of the file */
  223.     png_write_end(png_ptr, info_ptr);
  224.  
  225.     /* clean up after the write, and free any memory allocated */
  226.     png_write_destroy(png_ptr);
  227.  
  228.     /* if you malloced the palette, free it here */
  229.     if (info_ptr->palette)
  230.          gs_free(info_ptr->palette, 256 * sizeof (png_color), 1, "png palette");
  231.  
  232.     /* free the structures */
  233.     gs_free((char *)row, raster, 1, "png raster buffer");
  234.     gs_free((char *)png_ptr, sizeof (png_struct), 1, "png structure");
  235.     gs_free((char *)info_ptr, sizeof (png_info), 1, "png info_ptr");
  236.  
  237.     return code;
  238. }
  239.  
  240. /* 24-bit color mappers (taken from gdevmem2.c). */
  241. /* Note that Windows expects RGB values in the order B,G,R. */
  242.  
  243. /* Map a r-g-b color to a color index. */
  244. private gx_color_index
  245. map_16m_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  246.   gx_color_value b)
  247. {    return gx_color_value_to_byte(b) +
  248.            ((uint)gx_color_value_to_byte(g) << 8) +
  249.            ((ulong)gx_color_value_to_byte(r) << 16);
  250. }
  251.  
  252. /* Map a color index to a r-g-b color. */
  253. private int
  254. map_16m_color_rgb(gx_device *dev, gx_color_index color,
  255.   gx_color_value prgb[3])
  256. {    prgb[0] = gx_color_value_from_byte(color >> 16);
  257.     prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
  258.     prgb[2] = gx_color_value_from_byte(color & 0xff);
  259.     return 0;
  260. }
  261.  
  262.